<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="库存商品" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入库存商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新增任务量" prop="newAddTaskNum">
        <el-input
          v-model="queryParams.newAddTaskNum"
          placeholder="请输入新增任务量/吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产量" prop="outputNum">
        <el-input
          v-model="queryParams.outputNum"
          placeholder="请输入产量/吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出货量" prop="shipmentNum">
        <el-input
          v-model="queryParams.shipmentNum"
          placeholder="请输入出货量/吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建人姓名" prop="createByName">
        <el-input
          v-model="queryParams.createByName"
          placeholder="请输入创建人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="创建人部门" prop="createByDept">-->
<!--        <el-input-->
<!--          v-model="queryParams.createByDept"-->
<!--          placeholder="请输入创建人部门"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建人部门id" prop="createByDeptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.createByDeptId"-->
<!--          placeholder="请输入创建人部门id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建人id" prop="createById">-->
<!--        <el-input-->
<!--          v-model="queryParams.createById"-->
<!--          placeholder="请输入创建人id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留字段1" prop="reserveParam1">-->
<!--        <el-input-->
<!--          v-model="queryParams.reserveParam1"-->
<!--          placeholder="请输入预留字段1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留字段2" prop="reserveParam2">-->
<!--        <el-input-->
<!--          v-model="queryParams.reserveParam2"-->
<!--          placeholder="请输入预留字段2"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留字段3" prop="reserveParam3">-->
<!--        <el-input-->
<!--          v-model="queryParams.reserveParam3"-->
<!--          placeholder="请输入预留字段3"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留字段4" prop="reserveParam4">-->
<!--        <el-input-->
<!--          v-model="queryParams.reserveParam4"-->
<!--          placeholder="请输入预留字段4"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留字段5" prop="reserveParam5">-->
<!--        <el-input-->
<!--          v-model="queryParams.reserveParam5"-->
<!--          placeholder="请输入预留字段5"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建人部门" prop="createByDeptId" v-hasPermi="['factory_data_upload:factory_city_update_stock:list_all']">
        <el-select
            v-model="queryParams.createByDeptId"
            placeholder="请选择创建人部门"
            clearable
            @visible-change="getDepartments"
        >
          <el-option
              v-for="dept in deptList"
              :key="dept.deptId"
              :label="dept.deptName"
              :value="dept.deptId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
            v-hasPermi="['factory_data_upload:factory_city_update_stock:list_all','factory_data_upload:factory_city_update_stock:list','factory_data_upload:factory_city_update_stock:list_by_dept_id']"
            type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['factory_data_upload:factory_city_update_stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-if="!isChange"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['factory_data_upload:factory_city_update_stock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['factory_data_upload:factory_city_update_stock:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            size="mini"
            @click="changeTable"
            v-hasPermi="['factory_data_upload:factory_city_update_stock:change']"
        >切换
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="factory_city_update_stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="commodityId" />-->
      <el-table-column v-if="!isChange" label="库存商品" align="center" prop="commodityName" />
      <el-table-column v-if="!isChange"  label="新增任务量/吨" align="center" prop="newAddTaskNum" />
      <el-table-column v-if="!isChange"  label="产量/吨" align="center" prop="outputNum" />
      <el-table-column v-if="!isChange"  label="出货量/吨" align="center" prop="shipmentNum" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人姓名" align="center" prop="createByName" />
      <el-table-column label="创建人部门" align="center" prop="createByDept" />
<!--      <el-table-column label="创建人部门id" align="center" prop="createByDeptId" />-->
<!--      <el-table-column label="创建人id" align="center" prop="createById" />-->
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="状态标志" align="center" prop="status" />-->
<!--      <el-table-column label="预留字段1" align="center" prop="reserveParam1" />-->
<!--      <el-table-column label="预留字段2" align="center" prop="reserveParam2" />-->
<!--      <el-table-column label="预留字段3" align="center" prop="reserveParam3" />-->
<!--      <el-table-column label="预留字段4" align="center" prop="reserveParam4" />-->
<!--      <el-table-column label="预留字段5" align="center" prop="reserveParam5" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
              v-if="isChange"
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="getOneBatchInfoByRemark(scope.row.remark)"
              v-hasPermi="['factory_data_upload:factory_city_update_stock:info']"
          >详情
          </el-button>

          <el-button
              v-if="!isChange"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['factory_data_upload:factory_city_update_stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改城市更新（库存商品）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="601px" append-to-body>
      <el-table
          style="width: 100%"
          :data="ones"
          border
      >
        <el-table-column label="库存商品" prop="commodityName" width="140" align="center">
        </el-table-column>
        <el-table-column label="新增任务量/吨" prop="newAddTaskNum" width="140" align="center">
          <template slot-scope="scope">
            <el-input style="width: 100%;height: 100%" v-model="scope.row.newAddTaskNum"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="产量/吨" prop="outputNum" width="140" align="center">
          <template slot-scope="scope">
            <el-input style="width: 100%;height: 100%" v-model="scope.row.outputNum"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="出货量/吨" prop="shipmentNum" width="140" align="center">
          <template slot-scope="scope">
            <el-input style="width: 100%;height: 100%" v-model="scope.row.shipmentNum"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchAdd" :disabled="isDisabled">确 定</el-button>
        <el-button @click="cancel" :disabled="isDisabled">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 展示详情 -->
    <el-dialog :title="title" :visible.sync="oneBatchInfoOpen" width="601px" append-to-body>

      <div v-if="oneBatchInfo.length!==0">
        <el-tag style="margin-right: 10px">{{ oneBatchInfo[0].createByName }}</el-tag>
        <el-tag style="margin-right: 10px">{{ oneBatchInfo[0].createByDept }}</el-tag>
        <el-tag style="margin-right: 10px">{{ parseTime(oneBatchInfo[0].createTime) }}</el-tag>
      </div>
      <br/>
      <el-table
          style="width: 100%"
          :data="oneBatchInfo"
          border
      >
        <el-table-column label="库存商品" prop="commodityName" width="140" align="center">
        </el-table-column>
        <el-table-column label="新增任务量/吨" prop="newAddTaskNum" width="140" align="center">
        </el-table-column>
        <el-table-column label="产量/吨" prop="outputNum" width="140" align="center">
        </el-table-column>
        <el-table-column label="出货量/吨" prop="shipmentNum" width="140" align="center">
        </el-table-column>

      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeOneBatchInfo">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listFactory_city_update_stock, getFactory_city_update_stock, delFactory_city_update_stock, addFactory_city_update_stock, updateFactory_city_update_stock } from "@/api/factory_data_upload/factory_city_update_stock";
import { listDept } from '@/api/system/dept'
import { nanoid } from 'nanoid'
export default {
  name: "Factory_city_update_stock",
  data() {
    return {
      isDisabled:false,
        ones: [
          {
            commodityId: null,
            commodityName: "石膏砂浆",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "水泥砂浆",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "环氧树脂",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "聚氨酯",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "渗透结晶材料",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "抢修材料",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          },{
            commodityId: null,
            commodityName: "碳纤维布（平米）",
            newAddTaskNum: null,
            outputNum: null,
            shipmentNum: null,
            createTime: null,
            createByName: null,
            createByDept: null,
            createByDeptId: null,
            createById: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            status: null,
            reserveParam1: null,
            reserveParam2: null,
            reserveParam3: null,
            reserveParam4: null,
            reserveParam5: null
          }

        ],
        isChange:true,
      oneBatchInfo:[],
      oneBatchInfoOpen:false,
      //所有部门信息
      deptList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 城市更新（库存商品）表格数据
      factory_city_update_stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预留字段5时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityName: null,
        newAddTaskNum: null,
        outputNum: null,
        shipmentNum: null,
        createTime: null,
        createByName: null,
        createByDept: null,
        createByDeptId: null,
        createById: null,
        status: null,
        reserveParam1: null,
        reserveParam2: null,
        reserveParam3: null,
        reserveParam4: null,
        reserveParam5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    resetOnes(){
      // 需要修改
      this.ones=[
        {
          commodityId: null,
          commodityName: "石膏砂浆",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "水泥砂浆",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "环氧树脂",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "聚氨酯",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "渗透结晶材料",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "抢修材料",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        },{
          commodityId: null,
          commodityName: "碳纤维布（平米）",
          newAddTaskNum: null,
          outputNum: null,
          shipmentNum: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null,
          reserveParam1: null,
          reserveParam2: null,
          reserveParam3: null,
          reserveParam4: null,
          reserveParam5: null
        }

      ]
    },
    batchAdd(){
      this.$modal.msg('正在添加...')
      this.isDisabled = true
      new Promise((resolve, reject) => {
        let nanoId = nanoid()
        for (let i = 0; i < this.ones.length-1; i++) {
          this.ones[i].remark = nanoId
          addFactory_city_update_stock(this.ones[i])
        }
        this.ones[this.ones.length-1].remark = nanoId
        addFactory_city_update_stock(this.ones[this.ones.length-1]).then(()=>{
          this.getList()
        })
        resolve()
      }).then(() => {
        this.$modal.msgSuccess('新增成功')
        this.resetOnes()
        this.open = false
        this.isDisabled = false
      }).catch(() => {
        this.$modal.msgSuccess('新增失败')
        this.open = false
        this.isDisabled = false
      })

    },
    changeTable() {

      this.isChange = !this.isChange

      this.resetQuery()
      this.getList()
    },
    closeOneBatchInfo() {
      this.oneBatchInfoOpen = false
      this.oneBatchInfo = []
    },
    getOneBatchInfoByRemark(remark) {
      getFactory_city_update_stock(remark).then(response => {

        this.oneBatchInfo = response.data
        this.oneBatchInfoOpen = true
      }).catch(() => {
        this.$modal.msgError('获取失败')
      })
    },
    getDepartments() {
      if (this.deptList == null || this.deptList.length === 0) {
        listDept({}).then(response => {
          this.deptList = response.data
        })
      }
      return true
    },
    /** 查询城市更新（库存商品）列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      this.queryParams.isChange =this.isChange;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listFactory_city_update_stock(this.queryParams).then(response => {
        this.factory_city_update_stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commodityId: null,
        commodityName: null,
        newAddTaskNum: null,
        outputNum: null,
        shipmentNum: null,
        createTime: null,
        createByName: null,
        createByDept: null,
        createByDeptId: null,
        createById: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: null,
        reserveParam1: null,
        reserveParam2: null,
        reserveParam3: null,
        reserveParam4: null,
        reserveParam5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commodityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加城市更新（库存商品）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commodityId = row.commodityId || this.ids
      getFactory_city_update_stock(commodityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改城市更新（库存商品）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commodityId != null) {
            updateFactory_city_update_stock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactory_city_update_stock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityIds = row.commodityId || this.ids;
      this.$modal.confirm('是否确认删除城市更新（库存商品）编号为"' + commodityIds + '"的数据项？').then(function() {
        return delFactory_city_update_stock(commodityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('factory_data_upload/factory_city_update_stock/export', {
        ...this.queryParams
      }, `factory_city_update_stock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
